package com.wy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.wy.common.Result;
import com.wy.entity.GoodsLogistics;
import com.wy.service.GoodsLogisticsService;
import com.wy.service.UserInfoService;
import com.wy.util.EmailSender;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author:王渝
 * @Date: 2023/1/5  16:02
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/logistics")
public class GoodsLogisticsController {

    @Autowired
    private GoodsLogisticsService goodsLogisticsService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private EmailSender emailSender;

    /**
     * 发货
     * @param goodsLogistics 物流信息
     * @return
     */
    @RequestMapping("/dispatch")
    public Result<String> dispatchGoods(@RequestBody GoodsLogistics goodsLogistics){

        LambdaQueryWrapper<GoodsLogistics> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsLogistics::getOrderId,goodsLogistics.getOrderId());
        //查询是否发货
        GoodsLogistics one = goodsLogisticsService.getOne(lambdaQueryWrapper);
        if(one != null){
            return Result.error("你已经发过货了");
        }
        lambdaQueryWrapper.clear();
        lambdaQueryWrapper.eq(GoodsLogistics::getLogisticsName,goodsLogistics.getLogisticsName());
        lambdaQueryWrapper.eq(GoodsLogistics::getLogisticsId,goodsLogistics.getLogisticsId());
        //查询快递单号是否存在
        GoodsLogistics one1 = goodsLogisticsService.getOne(lambdaQueryWrapper);
        if(one1 != null){
            return Result.error("快递单号已经存在");
        }
        GoodsLogistics byId = goodsLogisticsService.getById(goodsLogistics.getLogisticsId());
        if(byId != null){
            return Result.error("你已经发货");
        }
        //状态2 表示已发货
        goodsLogistics.setStatus("2");
        goodsLogistics.setDeliveryTime(LocalDateTime.now());

        //插入数据库
        boolean save = goodsLogisticsService.save(goodsLogistics);
        if(save){
            //查询买方用户邮箱
            String email = userInfoService.queryEmailByOrderId(goodsLogistics.getOrderId());
            //发送邮箱提醒
            emailSender.send(email,"你好！<br>你购买的商品已经发货。<br>"
                    + goodsLogistics.getLogisticsName() +": " + goodsLogistics.getLogisticsId());
            return Result.success("发货成功");
        }
        return Result.error("发货失败");
    }

    /**
     * 根据订单id查询物流信息
     * @param orderId 订单id
     * @return
     */
    @RequestMapping("/check/{orderId}")
    public Result<GoodsLogistics> queryLogisticByIdOrderId(@PathVariable Long orderId){
        LambdaQueryWrapper<GoodsLogistics> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsLogistics::getOrderId,orderId);
        GoodsLogistics one = goodsLogisticsService.getOne(lambdaQueryWrapper);
        if(one == null){
            return Result.error("未发货");
        }
        return Result.success(one);
    }

    /**
     * 确认收货
     * @param orderId 订单id
     * @return
     */
    @RequestMapping("/receive/{orderId}")
    public Result<String> receive(@PathVariable Long orderId){
        LambdaQueryWrapper<GoodsLogistics> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsLogistics::getOrderId,orderId);
        lambdaQueryWrapper.select(GoodsLogistics::getStatus);
        GoodsLogistics one = goodsLogisticsService.getOne(lambdaQueryWrapper);
        if("3".equals(one.getStatus())){
            return Result.error("不能重复收货");
        }
        boolean receive = goodsLogisticsService.receive(orderId);
        if(receive){
            return Result.success("收货成功");
        }
        return Result.error("确认收货失败");
    }


}
